package boun.swe.bob.dao;
import boun.swe.bob.entities.HomeFeed;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

/**
 * This DAO corresponds to users home feeds
 */
@Component
public class HomeFeedDao  extends GenericDaoImpl<HomeFeed, Integer>{

    private static final Logger LOG = Logger.getLogger(HomeFeedDao.class);

    /**
     * This method is used for getting the home feeds of a user.
     *
     * @param UserId the integer user id value to get the home feeds
     * @return the result set containing the home feeds
     */
        public List<HomeFeed> findHomeFeedsByUserId(final Integer UserId, final Integer feedCount) {
        return (List<HomeFeed>) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from user_actions_view ua where (ua.user_id = ? or ua.actioner_user_id= ? or ua.user_id in (select user2_id from friends where approved=1 and user1_id = ? ) or ua.actioner_user_id in (select user2_id from friends where approved=1 and user1_id = ?)) and ua.action_type_id < 4  order by ua.action_id desc limit ?").addEntity("ua", HomeFeed.class);
                query.setInteger(0, UserId);
                query.setInteger(1, UserId);
                query.setInteger(2, UserId);
                query.setInteger(3, UserId);
                query.setInteger(4, feedCount);
                return query.list();
            }
        });
    }





}

